<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 2017/3/5
 * Time: 下午6:29
 */
class Auction extends MY_Www_Controller
{
    public function index()
    {
        $limit  = 18;
        $page   = intval($this->input->get_post('page'));
        $page   = $page ? $page : 1;
        $offset = ($page - 1) * $limit;
        $cid = (int)$this->input->get_post('cid');


        $where = array();
        $cid && $where['cid'] = $cid;


        $this->load->model('auction/Model_auction', 'a');
        $data = $this->a->lists($offset, $limit, $where);
        $total_rows = $this->a->count($where);

        //分页
        $this->load->library('pagination');

        $config['total_rows'] = $total_rows;
        $config['per_page'] = $limit;
        $config['base_url'] = '/Auction/index';

        $this->pagination->initialize($config);
        $page_html = $this->pagination->create_links();


        $this->load->model('auction/Model_auction_cate', 'ac');
        $cate_data = $this->ac->lists(OFFSET, LIMIT, array(), true, 'sort asc');


        $user_data = $uids = array();
        $uids = array_column($data, 'uid');


        if (!empty($uids))
        {
            $this->load->model('user/Model_user', 'u');
            $user_data = $this->u->getByIn('uid', $uids);
        }


        $data = array
        (
            'data'      => $data,
            'cid'       => $cid,
            'page_html' => $page_html,
            'cate_data' => $cate_data,
            'user_data' => $user_data,
        );


        $this->load->view('www/auction/index', $data);
    }


    public function detail()
    {
        $limit  = 20;
        $page   = intval($this->input->get_post('page'));
        $page   = $page ? $page : 1;
        $offset = ($page - 1) * $limit;

        $uid    = (int)$this->input->get_post('uid');


        if (!$uid)
        {
            show_error('参数不全!');
        }


        $this->load->model('user/Model_user', 'u');
        $user_item = $this->u->get_by_uid($uid, true);
        if (empty($user_item))
        {
            show_error('用户不存在!');
        }


        $where = array('uid' => $uid);


        $this->load->model('auction/Model_auction', 'a');
        $auction = $this->a->lists($offset, $limit, $where);
        $total_rows = $this->a->count($where);

        //分页
        $this->load->library('pagination');

        $config['total_rows'] = $total_rows;
        $config['per_page'] = $limit;
        $config['base_url'] = '/Auction/detail';

        $this->pagination->initialize($config);
        $show_page = $this->pagination->create_links();


        foreach ($auction as &$v)
        {
            $v['remain']        = strtotime($v['end_time']) - TIMESTAMP;
            $v['current_price'] = $this->a->current_price($v['aid']);
        }


        $data = array
        (
            'user_item' => $user_item['data'],
            'auction'   => $auction,
            'show_page' => $show_page,
        );


        $this->load->view('www/auction/detail', $data);
    }
}